<template>
  <section class="blog-recommend">
    <div class="blog-recommend-panel hot-tag">
      <h4 class="blog-recommend-title">
        热门标签
      </h4>
      <ul>
        <li v-for="item of hotTagList" :key="item.id" @click="changeCurrentTag(item.id, item.name)">
          # {{ item.name }}({{ getCount(item.count) }})
        </li>
      </ul>
    </div>
    <div class="blog-recommend-panel guess-you">
      <h4 class="blog-recommend-title">
        猜你喜欢
      </h4>
      <ul>
        <li v-for="item of guessYouList" :key="item.id">
          {{ item.lable }}
        </li>
      </ul>
    </div>
  </section>
</template>

<script>
export default {
  name: 'Recommend',
  props: {
    page: {
      type: String,
      default: () => 'index'
    }
  },
  data() {
    return {
      hotTagList: [],
      guessYouList: [{
        lable: 'Openresty lua 获取upstream 所有主机ip获取upstream 所有主机ip',
        id: '00012'
      }, {
        lable: '机器学习算法 攻克难疾的新救星',
        id: '00013'
      }, {
        lable: '研华发布全套32GB内存条：可耐85℃高温',
        id: '00015'
      }, {
        lable: 'Intel最近为Z390平台开放了单条32GB内存条的支持',
        id: '00016'
      }]
    }
  },
  mounted() {
    this.getHotTag()
  },
  methods: {
    async getHotTag() {
      const data = await this.$request({
        context: this,
        method: 'get',
        url: `tag/getHot/1/20`
      })
      if (!data) return
      this.hotTagList = data
    },
    getCount(number = 0) {
      if (number < 1000) {
        return number
      } else if (number >= 1000) {
        const count = number / 1000
        return count.toFixed(1) + 'k'
      }
    },
    changeCurrentTag(id, name) {
      if (this.page === 'index') {
        this.$router.push({
          path: `/tag/${id}`,
          query: {
            name
          }
        })
      } else if (this.page === 'tag') {
        this.$emit('changeCurrentTag', id, name)
      }
    }
  }
}
</script>

<style scoped lang="scss">
  @import "~@/assets/css/public";
  .blog-recommend {
    box-sizing: border-box;
    width: 300px;

    .blog-recommend-panel {
      overflow: hidden;
      min-height: 360px;
      margin-bottom: 12px;
      border: 1px solid $theme-color-border;
      background: #fff;

      &.hot-tag > ul {
        padding: 0 18px;
        display: flex;
        flex-wrap: wrap;

        li {
          cursor: pointer;
          font-size: 13px;
          color: $theme-color;
          opacity: .6;
          margin-right: 10px;
          margin-bottom: 12px;
          transition: $theme-transition;

          &:hover {
            opacity: 1;
          }
        }
      }

      &.guess-you > ul {
        padding-right: 22px;
        li {
          cursor: pointer;
          font-size: 13px;
          margin-bottom: 20px;
          @include one-line-text;
          transition: $theme-transition;

          &:before {
            content: "\25C6";
            display: inline-block;
            width: 18px;
            text-align: center;
            color: $theme-color;
          }

          &:hover {
            color: $theme-color;
          }
        }
      }

      h4.blog-recommend-title {
        font-size: 16px;
        height: 18px;
        line-height: 18px;
        position: relative;
        padding-left: 18px;
        margin: 12px 0 14px;

        &:before {
          content: "";
          display: inline-block;
          width: 4px;
          height: 18px;
          background: $theme-color;
          position: absolute;
          top: 0;
          left: 0;
        }
      }
    }

  }
</style>
